import pytest

from lib import ApiSession, ConfigParser, clean_yaml
from lib.setting.setting import BASE_PATH, TEMP_FILE


@pytest.fixture(scope='session')
def session():
    config = ConfigParser()
    base_url = config.get_base_url()
    session = ApiSession(base_url)
    request = {"method": 'POST', "url": '/login',
               "data": config.get_login_system_username_and_password()}
    response = session.request(**request)
    # 需要确定登录成功
    assert response.status_code == 200, '登录失败，状态码不为200'
    assert response.json()['code'] == 0, f'登录失败,code不为0,{response.json()["msg"]}'
    return session


@pytest.fixture(scope='session', autouse=True)
def clean_yaml_data():
    yield
    clean_yaml(BASE_PATH.joinpath(TEMP_FILE))
